package com.lc.controller;


import com.lc.mapper.AreaMapper;
import com.lc.pojo.Area;
import com.lc.pojo.Shop;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * @author gyb
 */
@Controller
public class LoginController {


    // 进入首页
    @RequestMapping({"/index","/"})
    public String toIndex(Model model){
        return "login";
    }

    // 跳转至登陆界面
    @RequestMapping({"/toLogin"})
    public String toLogin(){
        return "login";
    }

    // 点击登陆按钮的响应
    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        // 获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        // 封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);

            return "index";
        }catch (UnknownAccountException e){
            model.addAttribute("msg","用户名不存在");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }


    // 未授权响应
    @RequestMapping("/noauth")
    public String unauthorized(){
        return "401";

    }

    // 点击注销按钮的响应
    @RequestMapping("/loginOut")
    public String loginOut(Model model){
        // 获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        model.addAttribute("msg","您已注销");
        return "login";
    }

}
